#include "memory.h"
#include "ui_memory.h"
#include <QMessageBox>
#include <QString>
#include <QFileDialog>
#include <QDebug>
#include <QTextStream>
#include <QFile>
#include <QTextEdit>
#include <QDialog>
#include <QVBoxLayout>
#include "moments.h"
#include "ui_moments.h"

Memory::Memory(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::Memory)
{
    ui->setupUi(this);

    connect(ui->pushButton_3,SIGNAL(triggered(bool)),this,SLOT(on_pushButton_3_clicked(bool)));
}

Memory::~Memory()
{
    delete ui;
}

//关闭界面；
void Memory::on_pushButton_clicked()
{
    this->close();
}


void Memory::on_pushButton_3_clicked(bool checked)
{
    qDebug()<<"打开文件…"<<checked<<Qt::endl;
    // 弹出文件对话框，让用户选择文本文件
    QString fileName = QFileDialog::getOpenFileName(this, tr("Open Text File"), "", tr("Text Files (*.txt)"));
    if (!fileName.isEmpty()) {
        // 用户选择了文件，尝试打开并读取
        QFile file(fileName);
        if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
            // 无法打开文件，显示错误消息
            QMessageBox::warning(this, "Error", "Cannot open the file.");
            return;
        }

        // 使用 QTextStream 读取文件内容
        QTextStream in(&file);
        QString fileContent = in.readAll(); // 读取所有内容

        ui1 = new Ui::Moments;
        // 将文件内容设置到 QTextEdit 控件中
        ui1->textEdit->setPlainText(fileContent); // 假设你的 QTextEdit 对象名为 textEdit
    }
}

